package com.bwf.lgqingdan.gui.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bwf.lgqingdan.R;
import com.bwf.lgqingdan.gui.adapter.BaseFragmentPagerAdapter;
import com.bwf.lgqingdan.gui.fragment.SearchArticleListFragment;
import com.bwf.lgqingdan.gui.fragment.SearchThingListFragment;
import com.bwf.lgqingdan.gui.view.NestRadioGroup;
import com.bwf.lgqingdan.mvp.view.SearchResultView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/11/20.
 */

public class SearchResultActivity extends ActivityBase implements SearchResultView {
    public static final String TAG_KEYWORD = "keyword";
    private String keyword;
    @BindView(R.id.textView_button_cancel)
    TextView textViewButtonCancel;
    @BindView(R.id.editText_search)
    EditText editTextSearch;
    @BindView(R.id.img_search_delete)
    ImageView imgSearchDelete;
    @BindView(R.id.button_search)
    ImageButton buttonSearch;
    @BindView(R.id.root)
    LinearLayout root;
    @BindView(R.id.view_article_line)
    View viewArticleLine;
    @BindView(R.id.view_thing_line)
    View viewThingLine;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.radioGroup)
    NestRadioGroup radioGroup;

    private int[] tagLineViewIds = {R.id.view_article_line,R.id.view_thing_line};
    private View[] tagLineViews;
    private int[] radioButtonIds = {R.id.textView_tab_article,R.id.textView_tab_thing};

    private List<Fragment> fragments;
    @Override
    public int getContentViewResID() {
        return R.layout.activity_search_result;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        root.setPadding(0, getStatusHeight(), 0, 0);
        editTextSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    doSearch(v.getText().toString());
                }
                return false;
            }
        });
        editTextSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (TextUtils.isEmpty(s)) {
                    hideDeleteImg();
                } else {
                    showDeleteImg();
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        radioGroup.setOnCheckedChangeListener(new NestRadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(NestRadioGroup nestRadioGroup, int checkedId) {
                for (int i = 0; i < radioButtonIds.length; i++) {
                    if(checkedId == radioButtonIds[i]){
                        tagLineViews[i].setVisibility(View.VISIBLE);
                        viewPager.setCurrentItem(i,true);
                    }else{
                        tagLineViews[i].setVisibility(View.GONE);
                    }
                }
            }
        });
        tagLineViews = new View[tagLineViewIds.length];
        for (int i = 0; i < tagLineViewIds.length; i++) {
            tagLineViews[i] = findViewById(tagLineViewIds[i]);
        }

        viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                for (int i = 0; i < radioButtonIds.length; i++) {
                    if(position == i){
                        tagLineViews[i].setVisibility(View.VISIBLE);
                        radioGroup.setChecked(radioButtonIds[i]);
                    }else{
                        tagLineViews[i].setVisibility(View.GONE);
                    }
                }
            }
        });
    }
    /**
     * 执行搜索
     *
     * @param keyword
     */
    private void doSearch(String keyword) {
        if (!TextUtils.isEmpty(keyword)) {
            this.keyword = keyword;
            ((SearchArticleListFragment)fragments.get(0)).refresh(keyword);
            ((SearchThingListFragment)fragments.get(1)).refresh(keyword);
        }
    }
    @Override
    protected void initDatas() {
        keyword = getIntent().getStringExtra(TAG_KEYWORD);
        editTextSearch.getText().append(keyword);
        fragments = new ArrayList<>();
        fragments.add(SearchArticleListFragment.newInstance(keyword));
        fragments.add(SearchThingListFragment.newInstance(keyword));
        BaseFragmentPagerAdapter fragmentPagerAdapter = new BaseFragmentPagerAdapter(fragments,getSupportFragmentManager());
        viewPager.setAdapter(fragmentPagerAdapter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @OnClick({R.id.textView_button_cancel, R.id.img_search_delete})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.textView_button_cancel:
                clickCancel();
                break;
            case R.id.img_search_delete:
                clickDeleteImg();
                break;
        }
    }

    @Override
    public void clickCancel() {
        finish();
    }

    @Override
    public void clickDeleteImg() {
        editTextSearch.getText().clear();
    }

    @Override
    public void showDeleteImg() {
        imgSearchDelete.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideDeleteImg() {
        imgSearchDelete.setVisibility(View.GONE);
    }
}
